
package com.rt.schedulenew.core.support.shiro;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import com.rt.schedulenew.utils.jwt.util.JwtTokenUtil;
import com.rt.schedulenew.utils.util.EhCacheUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.util.WebUtils;
import java.io.Serializable;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
public class CustomSessionManager extends DefaultWebSessionManager
{
    private static final String AUTHORIZATION = "token";
    private static final String REFERENCED_SESSION_ID_SOURCE = "Stateless request";
    
    public CustomSessionManager() {
        this.setGlobalSessionTimeout(108000000L);
    }
    
    protected Serializable getSessionId(final ServletRequest request, final ServletResponse response) {
        String token = WebUtils.toHttp(request).getHeader(AUTHORIZATION);
        if (StringUtils.isNotBlank(token)) {
            EhCacheUtil.put(ConstantsUtil.EhCacheName.tokenCache.name(), token, (Serializable)token);
            String sessionId = JwtTokenUtil.getSessionId(token);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sessionId);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
            return sessionId;
        }
        return super.getSessionId(request, response);
    }
}
